package com.rtx.edi.platform.rule.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.api.enums.FieldValidateType;
import com.rtx.api.utils.FieldValidateUtils;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.baseinfo.bean.ApiStdRequest;
import com.rtx.edi.platform.rule.bean.TargetFieldConvertRule;
import com.rtx.edi.platform.rule.service.TargetFieldConvertRuleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@SuppressWarnings("all")
@Api(tags = {"目标字段转换规则"})
@RestController
@RequestMapping("/v1/targetFieldConvertRule")
public class TargetFieldConvertRuleController {
	
	@Autowired
	TargetFieldConvertRuleService targetFieldConvertRuleService;
	
	@Autowired
	ApplicationContext context;
	
    @ApiOperation(value = "queryActiveWhseid", notes = "获取EDI数据源")
    @PostMapping("/queryActiveWhseid")
    public EdiResponse queryActiveWhseid(@RequestBody TargetFieldConvertRule dto){
    	
    	String[] names = context.getBeanNamesForType(DataSource.class);
    	
    	List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		Map<String,String> datasource = new HashMap<String, String>();
		datasource.put("label", "");
		datasource.put("value", "");
		list.add(datasource);
		
		Map<String,String> datasource1 = new HashMap<String, String>();
		datasource1.put("label", "当前数据执行过程主数据源");
		datasource1.put("value", "current");
		list.add(datasource1);
		
		for(String name:names) {
			Map<String,String> datasourceTemp = new HashMap<String, String>();
			if("ris_edi".equals(name)) {
				datasourceTemp.put("label", "edi平台");
			}else if("enterprise".equals(name)) {
				datasourceTemp.put("label", "企业级");
			}else if(name.startsWith("wmwhse")){
				datasourceTemp.put("label", name.replace("wmwhse", "仓库"));
			}else {
				continue;
			}
			
			datasourceTemp.put("value", name);
			list.add(datasourceTemp);
		}
		
        return EdiResponse.success().data("item", list);
    }
    
    /**
     * 预览查询结构集合sql&data
     * @return
     */
    @ApiOperation(value = "预览查询集合", notes = "预览查询集合")
    @PostMapping("previewQuerySql")
    public EdiResponse previewQuerySqlAndData(@RequestBody TargetFieldConvertRule dto){
    	List<Map<String,String>> list = targetFieldConvertRuleService.previewData(dto);
        return EdiResponse.success().data("item",list);
    }
	
    /**
     * 获取wms标准报文目标字段转换规则
     * @return
     */
    @ApiOperation(value = "分页获取目标字段转换规则", notes = "分页获取目标字段转换规则")
    @PostMapping("selectTargetFieldConvertRulePageList")
    public EdiResponse selectTargetFieldConvertRulePageList(@RequestBody TargetFieldConvertRule dto){
        Page<TargetFieldConvertRule> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
        targetFieldConvertRuleService.selectList(pageParam,dto);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * 目标字段转换规则
     * @return
     */
    @ApiOperation(value = "目标字段转换规则", notes = "根据ID目标字段转换规则")
    @PostMapping("selectPlTargetFieldConvertRuleById")
    public EdiResponse selectPlTargetFieldConvertRuleById(@RequestBody TargetFieldConvertRule dto){
        return EdiResponse.success().data("item",targetFieldConvertRuleService.getById(dto.getId()));
    }

    /**
     * 新增目标字段转换规则
     * @return
     */
    @ApiOperation(value = "新增目标字段转换规则", notes = "新增目标字段转换规则")
    @PostMapping("addTargetFieldConvertRule")
    public EdiResponse addPlTargetFieldConvertRule(@RequestBody TargetFieldConvertRule dto){
    	try {
    		String querySql = targetFieldConvertRuleService.buildQuerySql(dto);
    		dto.setQuerymappingsql(querySql);
    		dto.setId(null);
    		targetFieldConvertRuleService.save(dto);
            return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
        
    }

    /**
     * 修改目标字段转换规则
     * @return
     */
    @ApiOperation(value = "修改目标字段转换规则", notes = "根据ID修改目标字段转换规则")
    @PostMapping("editTargetFieldConvertRule")
    public EdiResponse editTargetFieldConvertRule(@RequestBody TargetFieldConvertRule dto){
    	
    	try {
    		String querySql = targetFieldConvertRuleService.buildQuerySql(dto);
    		dto.setQuerymappingsql(querySql);
    		
    		targetFieldConvertRuleService.updateById(dto);
            return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
    	
    }

    /**
     * 删除目标字段转换规则
     * @return
     */
    @ApiOperation(value = "删除wms标准报文字段配置", notes = "根据ID删除wms标准报文字段配置")
    @PostMapping("deleteTargetFieldConvertRule")
    public EdiResponse deleteTargetFieldConvertRule(@RequestBody TargetFieldConvertRule dto){
        FieldValidateUtils.fieldNotNull(dto,FieldValidateType.DELETE);
        return  targetFieldConvertRuleService.removeById(dto.getId()) ? EdiResponse.success() : EdiResponse.failed();
    }

    /**
     * 批量删除目标字段转换规则
     * @return
     */
    @ApiOperation(value = "批量删除wms标准报文字段配置", notes = "批量删除wms标准报文字段配置")
    @PostMapping("deleteTargetFieldConvertRuleIds")
    public EdiResponse deleteTargetFieldConvertRuleByIds(@RequestBody List<TargetFieldConvertRule> list){
        List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
        return  targetFieldConvertRuleService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
    }
}
